<?php
/**
 * 《Arp信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: LogAction.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */

require_once(dirname(__FILE__) . '/LogUtil.class.php');

class ArpLog extends LogUtil {
	public $tbl = Constant::tbl_arp_log;

	public $groupby = array(
		'-' => '请选择',
		'ip' => 'IP',
		'mac' => 'MAC',
		'2' => '日期',
		'3' => '月份',
	);

	public $orderby = array(
		'-' => '请选择',
		'ip' => 'IP',
		'mac' => 'MAC',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/03/06 11:29:07 $';
		$request['revision'] = '$Revision: 1.2 $';

		parent::__construct($request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
			
		// $controller->assign('t1', strtotime('-1 week'));
		// $controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'log/arp/log/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$mac_s = $request['mac_s'];
		$ip_s = $request['ip_s'];

		$where = " WHERE 1=1";
		if (!empty($mac_s)) {
			$where .= " AND mac = '" . $mac_s . "'";
		}

		if (!empty($ip_s)) {
			$where .= " AND ip like '" . $ip_s . "%'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'log/arp/log/list');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row = $this->_view_t($row);
		
		$row['mac_x'] = $this->_emp_mac($row['mac']);

		return $row;
	}
	
	function _emp_mac($mac) {
		require_once(dirname(__FILE__) . '/../emp/Mac.class.php');
		$aa = new Mac();
		
		return $aa->_view_mac($mac);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-7 day'));
		$controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'log/action/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} elseif ($px == 'ip') {
			$row['a'] = $this->name_ip($row['a']);
		} else {
		
			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function name_ip($ip) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE ip = '%s'";
		$sql = sprintf($fmt, Constant::tbl_emp_mac, $ip);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			return sprintf("%s[%s]", Name::emp($row['emp']), $ip);
		}

		return $ip;
	}
	
	function t($controller, $request) {

		$file = "/home/sunxy/arp.txt";
		$fd = fopen($file, "r");
		while (!feof($fd)) {
			$buf = fgets($fd);
			// echo $buf;

			$this->_insert($buf);
		}
		fclose($fd);

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
		
	function nsz($controller, $request) {		
		$cmd = "/sbin/arp -n";		
		$s = shell_exec($cmd);
		$a = preg_split("/[\\n,]+/", $s);
		
		$i = 0;
		while (list($k, $buf) = each($a)) {
			if ($this->_insert($buf)) {
				$i++;
			}
		}
		
		if ($i > 0) {
			$request['a'] = '115';
			$request['ip'] = "192.168.2.%";
			$request['title'] = 'IP地址分配情况（南山）';
			
			$this->tjz($controller, $request);
		}
		
		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
	
	function sjz($controller, $request) {		
		$cmd = "/sbin/arp -n";		
		$s = shell_exec($cmd);
		$a = preg_split("/[\\n,]+/", $s);
		
		$i = 0;
		while (list($k, $buf) = each($a)) {
			if ($this->_insert($buf)) {
				$i++;
			}
		}
		
		if ($i > 0) {
			$request['a'] = '116';
			$request['ip'] = "192.168.3.%";
			$request['title'] = 'IP地址分配情况（沙井）';
			
			$this->tjz($controller, $request);
		}
		
		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}

	function ns($controller, $request) {
		$request['a'] = '115';
		$request['ip'] = "192.168.2.%";
		$request['title'] = 'IP地址分配情况（南山）';
		
		$this->tj($controller, $request);
	}
	
	function sj($controller, $request) {
		$request['a'] = '116';
		$request['ip'] = "192.168.3.%";
		$request['title'] = 'IP地址分配情况（沙井）';
		
		$this->tj($controller, $request);
	}
	
	function tjz($controller, $request) {
		$bb = $this->_list_email($request['a']);
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			// echo "222";
			return false;
		}

		$from = $this->email;
		$subject = $request['title'];
		$ip = $request['ip'];
		
		$cc = array(	
			'新增信息'=>array('w' => "ip LIKE '$ip' AND DATE_FORMAT(createtime, '%Y-%m-%d') = DATE_FORMAT(now(), '%Y-%m-%d')", 'b'=>'id DESC'),
			// '更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'id DESC'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}

		$body = $this->_tj_html($controller, $request, 'log/arp/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function tj($controller, $request) {
		$bb = $this->_list_email($request['a']);
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			// echo "222";
			return false;
		}

		$from = $this->email;
		$subject = $request['title'];
		$ip = $request['ip'];
		
		$cc = array(	
			'新增信息'=>array('w' => "ip LIKE '$ip' AND DATE_FORMAT(createtime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')", 'b'=>'id DESC'),
			// '更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'id DESC'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}

		$controller->clear_assign('result');
		
		$body = $this->_tj_html($controller, $request, 'log/arp/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function _insert($a) {
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, ip, mac, name, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		
		reset($a);
		while (list($k, $row) = each($a)) {			
			$emp = $this->_input($row['emp']);
			$office = $this->_office($emp);
			$dept = $this->_emp_dept($emp);
			
			$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $row['ip'], $row['mac'], $row['category_t']);
			debug($sql);
			$db->query($sql);
		}
	}
}
?>
